home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
-
- PROGRAM
- MenuMem - A combination INIT/DRVR that displays the amount of
- free memory in the menu bar.
-
- FILE
- “MenuMem INIT.c”
-
- DESCRIPTION
- This file contains all of the code for the INIT portion of the program.
- It loads the DRVR and its associated DATA into the system heap.
-
- HISTORY
- 2/14/90 - DAG Version .1
-
- Copyright © 1990 Daniel A. Green. All rights reserved.
-
- ------------------------------------------------------------------------------ */
-
-
-
- /* --------------------------------- INCLUDE FILES --------------------------- */
- #include "MacTypes.h"
- #include "string.h"
- #include "MenuMgr.h"
- #include <DeviceMgr.h>
- #include <ResourceMgr.h>
- #include "MenuMem.h"
-
-
-
- /* --------------------------------- PROTOTYPES ---------------------------- */
- Boolean DRVRXistsP(Integer theID);
- Boolean slot_takenP(Integer slot_num);
- Boolean vacant_slotP(Integer sun, Integer *the_slot);
-
-
-
- /* --------------------------------- FUNCTIONS ----------------------------- */
- pascal void main()
- {
- Integer currentID, newID;
- ResType r_type;
- Str255 r_name;
- OSErr theErr;
- Handle theDATA, theDRVR, theINIT;
-
- theINIT = GetNamedResource('INIT', DRVR_NAME);
- GetResInfo(theINIT, ¤tID, &r_type, &r_name);
-
- /*******************************************************************************
- *
- * If there is an installed DRVR with the same ID as ours, then the resource
- * ID of our DRVR and its DATA should be changed.
- *
- *******************************************************************************/
- if ( DRVRXistsP(currentID) )
- if ( vacant_slotP(DRVR_RSRCID, &newID) ) {
-
- SetResInfo(theINIT, newID, &r_name);
-
- theDRVR = GetResource('DRVR', currentID);
- GetResInfo(theDRVR, ¤tID, &r_type, &r_name);
- SetResInfo(theDRVR, newID, &r_name);
-
- theDATA = GetResource('DATA', OWNEDRSRCID( REFNUM(currentID) ) );
- GetResInfo(theDATA, ¤tID, &r_type, &r_name);
- SetResInfo(theDATA, OWNEDRSRCID( REFNUM(newID) ), &r_name);
-
- currentID = newID;
- } else { /* Max number of DRiVeRs installed. Just beep and return */
- SysBeep(1);
- return;
- }
-
- /* If we made it this far we can open the driver up */
- if ( LoadDRVR(&r_name, currentID) ) SysBeep(2);
- };
-
-
-
- /*******************************************************************************
- LoadDRVR
- * Loads the DRiVeR into memory and opens it. The System Heap attribute for
- * both the DRVR and DATA resources should be set to insure that neither will
- * be lost when the application heap is reinitialized.
- *
- *******************************************************************************/
-
- LoadDRVR(theName, theID)
- Str255 *theName;
- Integer theID;
- {
- Integer theRefNum;
- OSErr theErr;
- ResType theType;
- Handle h, theDRVR;
-
- h = GetResource('DRVR', theID);
- theErr = RESERROR();
- if (h && (theErr == noErr) )
- if ( (theErr = OpenDriver(theName, &theRefNum)) == noErr )
- DetachResource(h);
-
- return(theErr);
- };
-
-
-
- /*******************************************************************************
- DRVRXistsP
- * returns TRUE if a DRiVeR with an ID of ‘theID’ exists in the resource fork
- * of the System File or a DRiVeR with an ID of ‘theID’ is in the Unit Table.
- * Otherwise returns FALSE.
- *
- *******************************************************************************/
-
- Boolean DRVRXistsP(theID)
- Integer theID;
- {
- Boolean result = FALSE;
- Integer theRefNum;
- Handle h;
-
- /* Save the reference number of our resource file */
- theRefNum = CurResFile();
- UseResFile(0); /* Use the System resource file */
- SetResLoad(FALSE);
- h = GetResource('DRVR', theID);
-
- /* If we can’t get a handle to the resource, then check the unit table */
- if ( h || slot_takenP(theID) ) result = TRUE;
- if (h) ReleaseResource(h);
- SetResLoad(TRUE);
- UseResFile(theRefNum);
- return(result);
- };
-
-
-
- /*******************************************************************************
- slot_takenP
- * If we can get a DCtlEntry for a given unit number, then a DRiVeR with the
- * given unit number has already been installed, so the function returns TRUE.
- * Otherwise it returns FALSE.
- *
- *******************************************************************************/
-
- Boolean slot_takenP(unit_number)
- Integer unit_number;
- {
- return( GetDCtlEntry( REFNUM( unit_number ) ) ? TRUE : FALSE );
- };
-
-
-
- /*******************************************************************************
- vacant_slotP
- * Repeatedly searches the System file and checks the unit table until an ID
- * has been found for which:
- * 1). No resource of type DRVR exists in the System file, and
- * 2). No entry exists in the unit table.
- * If an ID that satisfies the above criteria is found, the function sets
- * the_slot to the ID and returns TRUE. Otherwise it returns FALSE.
- *
- *******************************************************************************/
-
- Boolean vacant_slotP(sun, the_slot)
- Integer sun; /* the Starting Unit Number */
- Integer *the_slot;
- {
- Boolean vacant_slot = FALSE;
- Integer i, max, refnum, theRefNum;
- Handle h;
-
- max = *(Integer *) (UnitNtryCnt);
-
- theRefNum = CurResFile();
- UseResFile(0); /* Use the System resource file */
- SetResLoad(FALSE); /* Don’t want to load the resources */
- /* Just check for their existence */
- i = sun;
- do {
- h = GetResource('DRVR', i);
- if (h == NULL) { /* Check the unit table */
- refnum = (-i) - 1;
- if ( GetDCtlEntry(refnum) == NULL) {
- vacant_slot = TRUE;
- *the_slot = i;
- }
- } else
- ReleaseResource(h);
- i++;
- } while ( !vacant_slot && (i < max) );
-
- SetResLoad(TRUE);
- UseResFile(theRefNum); /* Reset the search path */
-
- return(vacant_slot);
- };
-